﻿using FileManagerSystem.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FileManagerSystem.Extend
{
    /// <summary>
    /// 功能描述    ：ControllerExtend
    /// </summary>
    public static class ControllerExtend
    {
        #region 针对JsonResult的扩展

        /// <summary>
        /// 直接返回obj实体
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static JsonResult JsonResult(this Controller controller, object obj)
        {
            return controller.Json(obj);
        }

        /// <summary>
        /// 直接返回JsonDataModel作为实体类
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static JsonResult ObjJsonResult(this Controller controller, JsonDataModel obj)
        {
            return controller.Json(obj);
        }

        /// <summary>
        /// 返回正常Code==0的结果
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static JsonResult OkObjJsonResult(this Controller controller, object obj = null)
        {
            return controller.Json(JsonDataModel.Ok(obj));
        }

        /// <summary>
        /// 返回异常Code！=0的结果
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="tip"></param>
        /// <returns></returns>
        public static JsonResult ErrorObjJsonResult(this Controller controller, string tip)
        {
            return controller.Json(JsonDataModel.Error(tip));
        }

        #endregion 针对JsonResult的扩展

        #region Api返回结果扩展

        /// <summary>
        /// 基于controller扩展将数据object 对象转化成json数据
        /// </summary>
        /// <param name="controller">扩展</param>
        /// <param name="obj">直接返回相关的Action结果</param>
        /// <returns></returns>
        public static IActionResult ApiJsonResult(this ControllerBase controller, object obj)
        {
            //new JsonResult();
            return controller.Ok(obj);
        }

        /// <summary>
        /// ApiAction正常返回扩展
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static IActionResult OkApiResult(this ControllerBase controller, object obj)
        {
            return controller.Ok(JsonDataModel.Ok(obj));
        }

        /// <summary>
        /// ApiAction异常返回扩展
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="msg">异常提示信息</param>
        /// <returns></returns>
        public static IActionResult ErrorApiResult(this ControllerBase controller, string msg = "程序处理异常，请稍后重试!")
        {
            return controller.Ok(JsonDataModel.Error(msg));
        }

        /// <summary>
        /// 返回JsonResult-成功
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="obj">返回结果</param>
        /// <returns></returns>
        public static IActionResult JsonResultOk(this ControllerBase controller, object obj)
        {
            return new JsonResult(JsonDataModel.Ok(obj));
        }

        /// <summary>
        /// 返回JsonResult-异常
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="msg">异常信息提示</param>
        /// <returns></returns>
        public static IActionResult JsonResultError(this ControllerBase controller, string msg = "程序处理异常，请稍后重试!")
        {
            return new JsonResult(JsonDataModel.Error(msg));
        }

        #endregion Api返回结果扩展

        #region ActionExecutingContext上下文扩展

        /// <summary>
        /// ActionExecutingContext上下文ModelStateDictionary扩展
        /// </summary>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public static string ModelValSummary(this ModelStateDictionary modelState)
        {
            StringBuilder sb = new StringBuilder("反馈提示:");
            try
            {
                if (modelState.ErrorCount > 0)
                {
                    int index = 1;
                    foreach (var item in modelState)
                    {
                        if (item.Value.Errors.Any())
                        {
                            //存在异常,获取所有的异常信息集合
                            string errDetail = string.Join(';', item.Value.Errors.Select(s => s.ErrorMessage));

                            sb.AppendFormat("({0})、属性:{1}异常，异常提示:{2}", index, item.Key, errDetail);
                            index++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                sb.AppendFormat("请求参数异常,获取异常信息时报错，错误信息如下:{0}", ex.Message);
            }

            return sb.ToString();
        }

        #endregion ActionExecutingContext上下文扩展
    }

    
}
